package com.tengju.bff.interfaces.manage.member.article;

import com.tengju.bff.interfaces.manage.member.article.facade.ArticleFacade;
import com.tengju.bff.interfaces.manage.member.article.vo.*;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.support.application.article.ArticleService;
import com.tengju.support.domain.model.article.ArticleId;
import com.tengju.support.domain.model.article.ArticleType;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import static com.tengju.user.domain.shared.ResultCodeEnum.SYS_EXCEPTION;

@RestController
@Api(tags = "会员管理-文章管理接口")
@RequestMapping(value = "manage/member/article/")
@Slf4j
@ManageEntrance
public class ArticleController {

    private final ArticleService articleService;
    private final ArticleFacade articleFacade;

    public ArticleController(ArticleService articleService, ArticleFacade articleFacade) {
        this.articleService = articleService;
        this.articleFacade = articleFacade;
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "添加文章", notes = "添加文章")
    @RequestMapping(value = "add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> addArticle(@RequestBody AddArticleForm form) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            articleService.add(form.getTitle(),
                    ArticleType.fromVal(form.getType()), form.getContext(), staff.getName());
            return ApiResponse.newSuccess();
        } catch (DomainException | ApplicationException e) {
            log.error("添加文章失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("添加文章异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "修改文章", notes = "修改文章")
    @RequestMapping(value = "edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> editArticle(@RequestBody EditArticleForm form) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            articleService.edit(new ArticleId(form.getArticleId()), form.getTitle(),
                    ArticleType.fromVal(form.getType()), form.getContext(), staff.getName());
            return ApiResponse.newSuccess();
        } catch (DomainException | ApplicationException e) {
            log.error("编辑文章失败,articleId:{}", form.getArticleId(), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("编辑文章异常,articleId:{}", form.getArticleId(), e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }


    @RequiresRoles("sso")
    @ApiOperation(value = "删除文章", notes = "删除文章")
    @RequestMapping(value = "del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> delArticle(@RequestBody DelArticleForm form) {
        try {
            articleService.del(new ArticleId(form.getArticleId()));
            return ApiResponse.newSuccess();
        } catch (DomainException | ApplicationException e) {
            log.error("删除文章失败,articleId:{}", form.getArticleId(), e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("删除文章异常,articleId:{}", form.getArticleId(), e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "分页查询文章", notes = "分页查询文章")
    @RequestMapping(value = "getByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<TableVO> getArticleByPage(GetArticleForm form) {
        return ApiResponse.newSuccess(articleFacade.getArticleByPage(form));
    }

    @NotTokenValid
    @ApiOperation(value = "获取文章上下文", notes = "获取文章上下文")
    @RequestMapping(value = "getContext", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<ArticleContextVO> getArticleContext(@RequestParam Long articleId) {
        try {
            return ApiResponse
                    .newSuccess(articleFacade.getArticleContext(new ArticleId(articleId)));
        } catch (DomainException | ApplicationException e) {
            log.error("获取文章上下文失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取文章上下文异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }
}
